#include "myscene.h"
#include <QThread>

MyScene::MyScene(QObject *parent) :
    QGraphicsScene(parent)
{
    //背景
    this->setBackgroundBrush(QPixmap(":/resources/road.png"));
    this->setPos( 0, 0);

    //小车
    mycar = new MyItem;
    car1 = new Item;
    car2 = new Item;
    car3 = new Item;
    car4 = new Item;

    //小车添加进场景
    this->addItem(mycar);
    this->addItem(car1);
    this->addItem(car2);
    this->addItem(car3);
    this->addItem(car4);

    //加载小车图片
    car1->setPix(QPixmap(":/resources/car1.png"));
    car2->setPix(QPixmap(":/resources/car2.png"));
    car3->setPix(QPixmap(":/resources/car3.png"));
    car4->setPix(QPixmap(":/resources/car4.png"));

    //关联槽函数
    connect(mycar, SIGNAL(sendSpeed(qreal)), this, SLOT(GetSpeed(qreal)));
    connect(mycar, SIGNAL(ending()), this, SLOT(gameOver()));
    connect(this, SIGNAL(SendDy(qreal)), car1, SLOT(GetDy(qreal)));
    connect(this, SIGNAL(SendDy(qreal)), car2, SLOT(GetDy(qreal)));
    connect(this, SIGNAL(SendDy(qreal)), car3, SLOT(GetDy(qreal)));
    connect(this, SIGNAL(SendDy(qreal)), car4, SLOT(GetDy(qreal)));

    //设置面板
    option = new MyOption;
    proxy = new QGraphicsProxyWidget;
    proxy = this->addWidget(option);
    connect( this, SIGNAL(SendDy(qreal)), option, SLOT(GetDy(qreal)));
    connect( this, SIGNAL(over()), option, SLOT(gameOverStop()));
    connect( option, SIGNAL(pause()), this, SLOT(pauseGame()));
    connect( mycar, SIGNAL(ending()), option, SLOT(gameOverStart()));

    //定时器
    timer = new QTimer;
    connect( timer, SIGNAL(timeout()), this, SLOT(work()));
    connect( timer, SIGNAL(timeout()), mycar, SLOT(work()));
}

/**
 * @brief 获得控制的小车速度
 *
 * @param Speed 控制的小车速度
 */
void MyScene::GetSpeed(const qreal &Speed)
{
    this->speed = Speed;
}

/**
 * @brief 开始游戏
 *
 */
void MyScene::beginGame()
{
    //初始化玩家小车
    mycar->setData( 1, 1);
    mycar->setPos( 260, 500);
    mycar->setFocus();
    mycar->initParam();
    mycar->initSpeed();
    mycar->show();

    //初始化赛道
    this->setPos( 0, 0);
    this->speed = mycar->getMinSpeed();
    this->dy = 0;

    //初始化NPC小车
    car1->setBase( 265, 1700);
    car2->setBase( 170, 2000);
    car3->setBase( 90, 2400);
    car4->setBase( 344, 2800);

    StatusValue::status = Playing;
    //启动定时器
    timer->start(0.5);
}

/**
 * @brief 暂停游戏
 *
 */
void MyScene::pauseGame()
{
    timer->stop();
    StatusValue::status = Pausing;
    emit pause();
}

/**
 * @brief 继续游戏
 *
 */
void MyScene::continueGame()
{
    StatusValue::status = Playing;
    mycar->setFocus();
    timer->start();
}

/**
 * @brief 游戏结束
 *
 */
void MyScene::gameOver()
{
    timer->stop();
    StatusValue::status = Ready;

    // 2秒单触发定时器
    QTimer *time = new QTimer;
    time->singleShot( 2520, this, SIGNAL(over()));
}

/**
 * @brief 设置场景坐标
 *
 * @param x 横坐标
 * @param y 纵坐标
 */
void MyScene::setPos(const qreal &x, const qreal &y)
{
    this->setSceneRect( x, y, 480, 800);
}

/**
 * @brief 移动场景相对视图的位置
 *
 */
void MyScene::work()
{
    this->setPos( 0, dy = dy - speed);
    proxy->setPos( 0, dy);
    emit SendDy(dy);
}
